package com.baobao.action;


import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import com.baobao.domain.Bbevent;
import com.baobao.domain.Parent;
import com.baobao.domain.Student;
import com.baobao.service.BbeventService;
import com.baobao.service.ParentService;
import com.baobao.service.StudentService;
import com.tencent.xinge.ClickAction;
import com.tencent.xinge.Message;
import com.tencent.xinge.MessageIOS;
import com.tencent.xinge.Style;
import com.tencent.xinge.TimeInterval;
import com.tencent.xinge.XingeApp;
import com.tencent.xinge.TagTokenPair;

@Controller
@RequestMapping("/event")
public class EventAction {

	private static Logger log = LoggerFactory.getLogger(EventAction.class);
	
	@Autowired
	private BbeventService eventService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ParentService parentService;
	
	/**
	 * 获取事件列表
	 */
	@RequestMapping(value="/get",  produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String GetEvents(HttpServletRequest request,
		@RequestParam(value="bbid",required=true) String bbId,
		@RequestParam(value="date",required=true) String date){
		
		JSONObject result = new JSONObject();
		String startTime = date + " 00:00:00";
		String endTime = date + " 23:59:59";
		
		Set<Bbevent> eventSet = eventService.findBbeventByDate(bbId,startTime, endTime);

		result.put("success",true);
		result.put("message", eventSet);
		
		return result.toString();				
	}
	
	/**
	 * 清空学生当日的事件列表
	 */
	@RequestMapping(value="/clear",  produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String ClearEvents(HttpServletRequest request,
		@RequestParam(value="bbid",required=true) String bbId,
		@RequestParam(value="date",required=true) String date){
		
		JSONObject result = new JSONObject();
		String startTime = date + " 00:00:00";
		String endTime = date + " 23:59:59";
		
		Set<Bbevent> eventSet = eventService.findBbeventByDate(bbId,startTime, endTime);
		for (Bbevent bbevent : eventSet) {
			eventService.deleteBbevent(bbevent);
		}

		result.put("success",true);
		result.put("message", eventSet);
		
		return result.toString();				
	}
	
	/**
	 * 增加模拟事件
	 */
	@RequestMapping(value="/set",  produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	@ResponseBody
	public String SetEvents(HttpServletRequest request,
		@RequestParam(value="bbid",required=true) String bbId,
		@RequestParam(value="date",required=true) String date,
		@RequestParam(value="type",required=true) String type){
		
		JSONObject result = new JSONObject();
		
		//根据bbId查找学生
		Set<Student> students = studentService.findStudentByBbId(bbId);
		Student student = null;
		for (Student student_tmp : students) {
			student = student_tmp;
		}
		
		if(student == null)
		{
			result.put("success",false);
			return result.toString();
		}
		String leaveOrEnter = (type.equals("0")) ? "进入" : "离开";
		String content = "尊敬的家长：您的孩子"+student.getName()+"于今日" + date + leaveOrEnter + "学校，详情请打开抱抱网APP查看";
		
		//遍历所有家长
		Set<Parent> parents = parentService.findParentByBbid(bbId);
		for (Parent parent : parents) {
			
			log.info("发送推送消息给家长："+parent.getName());
			//依次发送系统推送
			System.out.println(XingeApp.pushAccountIos(2200204681L, "1a9e3c99f14fc7875434d9801570bcdc", content, parent.getPhone(), XingeApp.IOSENV_DEV));
		}
				
		Bbevent event = new Bbevent();
		event.setBbId(bbId);
		event.setType(Integer.parseInt(type));
		event.setTime(date);
		
		eventService.saveBbevent(event);

		result.put("success",true);
	
		return result.toString();				
	}
	
}
